package nige.transport.core;

import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import nige.utils.Characters;

public class RecupStations {
	
	public static Vector STATIONNOM = null;
	public static Vector STATIONID = null;
	public static Vector STATION = null;
	
	public static void recuperer() {
		String liste = (new RecupStations()).lireFichier();
		formatListe(liste);
	}
	
	private String lireFichier() {
		StringBuffer sb = new StringBuffer();
		try {
			InputStream is = getClass().
				getResourceAsStream("stations.txt");
			int chars;
			while ((chars = is.read()) != -1){
				sb.append((char) chars);
			}
		}
		catch(Exception e) {
			System.out.println("lireFichier : "+e);
		}
		return sb.toString();
	}
	
	private static void formatListe(String liste) {
		String[] stations = Characters.split(liste, "\n");
		STATION = new Vector();
		STATIONNOM = new Vector();
		STATIONID = new Vector();
		for(int i=0;i<stations.length;i++) {
			String[] details = Characters.split(stations[i], ";");
			Station sta = new Station();
			try {
				sta.ligne = details[0];
				sta.id = Integer.parseInt(details[1]);
				sta.nom = details[2];
				sta.sens = Integer.parseInt(details[3]);
				for(int j=4;!details[j].trim().equals("");j++) {
					if(!details[j].equals("")) {
						sta.changement.addElement(details[j].trim());
					}
				}
			}
			catch(Exception e) {
			}
			if(!sta.nom.equals("")) {
				STATION.addElement(sta);
				STATIONNOM.addElement(sta.nom);
				STATIONID.addElement(sta.ligne+"|"+sta.id);
			}
		}
	}
	
	public static void dessine(Graphics graph,int xPos,int yPos,int zoom) 
			{
		if(STATION == null) {
			recuperer();
		}
		graph.setColor(0);
		graph.drawRect(10,10,5,5);
		graph.drawLine(10,10,5,5);
		Station lastStat = new Station();
		int i=0;
		for(i=0;i<STATION.size();i++) {
			Station statTmp = (Station)STATION.elementAt(i);
			graph.setColor(0);
			graph.drawRect(statTmp.xPos/6-1, statTmp.yPos/6-1, 2, 2);
			if(lastStat.ligne.equals(statTmp.ligne)) {
				graph.drawLine(lastStat.xPos/6,lastStat.yPos/6,
						statTmp.xPos/6,statTmp.yPos/6);
			}
			lastStat = statTmp;
		}
	}
	
}

